<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/5
 * Time: 16:24
 */

namespace iBrand\Shop\Backend\Http\Controllers;

use iBrand\Shop\Backend\Model\ShopBrandRelation;
use iBrand\Shop\Backend\Repository\StoreRepository;
use iBrand\Shop\Core\Models\Brand;
use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class BrandController extends Controller
{
    protected $shopRepository;

    public function __construct(StoreRepository $shopRepository)
    {
        $this->shopRepository = $shopRepository;
    }

    public function index()
    {
        $shop_id = session('shop_id');
        $shop = $this->shopRepository->find($shop_id);

        return LaravelAdmin::content(function (Content $content) use ($shop, $shop_id) {

            $content->header('门店品牌管理');

            $content->breadcrumb(
                ['text' => '门店管理', 'url' => 'shop/list/store', 'no-pjax' => 1],
                ['text' => '门店品牌管理', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']

            );

            $content->body(view('shop-backend::brand.index', compact('shop', 'shop_id')));
        });

    }

    public function create()
    {
        $shop_id = session('shop_id');
        $brands = Brand::where('is_show', 1)->get();

        return LaravelAdmin::content(function (Content $content) use ($brands, $shop_id) {

            $content->header('添加门店品牌');

            $content->breadcrumb(
                ['text' => '门店管理', 'url' => 'shop/list/store', 'no-pjax' => 1],
                ['text' => '门店品牌管理', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']

            );

            $content->body(view('shop-backend::brand.create', compact('brands', 'shop_id')));
        });
    }

    public function store(Request $request)
    {
        $shop_id = $request->input('shop_id');
        $brand_id = $request->input('brand_id');
        $discount = $request->input('discount');

        if (!$brand_id OR !$discount) {
            return $this->ajaxJson(false, [], 400, '请完善品牌信息');
        }

        if ($discount > 100 OR $discount < 10 OR !is_numeric($discount)) {
            return $this->ajaxJson(false, [], 400, '销售折扣输入错误');
        }


        $where = ['shop_id' => $shop_id, 'brand_id' => $brand_id];
        if ($id = $request->input('id')) {
            $where['id'] = ['<>', $request->input('id')];
        }

        if (ShopBrandRelation::where($where)->count()) {
            return $this->ajaxJson(false, [], 400, '该品牌已添加');
        }

        if ($request->input('id')) {
            $shopBrand = ShopBrandRelation::find($request->input('id'));
            $shopBrand->discount = $discount;
            $shopBrand->brand_id = $brand_id;
            $shopBrand->save();
        } else {
            ShopBrandRelation::create([
                'shop_id' => $shop_id,
                'brand_id' => $brand_id,
                'discount' => $discount
            ]);
        }
        return $this->ajaxJson();

    }

    public function edit($id)
    {
        $shop_id = session('shop_id');
        $brands = Brand::where('is_show', 1)->get();
        $shopBrand = ShopBrandRelation::find($id);

        return LaravelAdmin::content(function (Content $content) use ($brands, $shop_id, $shopBrand) {

            $content->header('门店品牌管理');

            $content->breadcrumb(
                ['text' => '门店管理', 'url' => 'shop/list/store', 'no-pjax' => 1],
                ['text' => '门店品牌管理', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '品牌管理']

            );

            $content->body(view('shop-backend::brand.edit', compact('brands', 'shopBrand', 'shop_id')));
        });
    }
}